package com.bruce.testxlog;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import timber.log.Timber;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明:测试xlog和mylog写入日志12
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //23以上要加权限
        checkPermission();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LogUtil.d("ccc");
        LogUtil.d("无返回参数2");
        LogUtil.d("无返回参数34");
        LogUtil.d("无返回参数1");

        MyLog.d("cc","1111");
        MyLog.d("cc","44444");

        Timber.d("测试一下log测试一下log测试一下log测试一下log测试一下log测试一下log" +
                "测试一下log测试一下log测试一下log测试一下log测试一下logcc测试一下log测试一下log测试一下log测试一下log测试一下logcc测试一下log测试一下log测试一下log测试一下log测试一下logcc测试一下log测试一下log测试一下log测试一下log测试一下logcc测试一下log测试一下log测试一下log测试一下log测试一下logcc测试一下log测试一下log测试一下log测试一下log测试一下logcc11");
        Timber.e("测试一下log1");
        Timber.i("测试一下log2");
        Timber.v("测试一下log3");
        Timber.tag("测试一下log0");
//        Timber.d("测试一下","cccc",mVolumeBarMax);
//        Timber.d("测试一下",mVolumeBarMax);

        // LogNotTimber
        Log.d("TAG", "msg");
        Log.d("TAG", "msg", new Exception());
        android.util.Log.d("TAG", "msg");
        android.util.Log.d("TAG", "msg", new Exception());

        // StringFormatInTimber
//        Timber.w(format("%s", getString()));

        // ThrowableNotAtBeginning
        Timber.d("%s", new Exception());

        // BinaryOperationInTimber
        String foo = "foo";
        String bar = "bar";
        Timber.d("foo" + "bar");
        Timber.d("foo" + bar);
        Timber.d(foo + "bar");
        Timber.d(foo + bar);

        // TimberArgCount
//        Timber.d("%s %s", "arg0");
//        Timber.d("%s", "arg0", "arg1");
//        Timber.tag("tag").d("%s %s", "arg0");
//        Timber.tag("tag").d("%s", "arg0", "arg1");

        // TimberArgTypes
//        Timber.d("%d", "arg0");
//        Timber.tag("tag").d("%d", "arg0");
//
//        // TimberTagLength
//        Timber.tag("abcdefghijklmnopqrstuvwx");
//        Timber.tag("abcdefghijklmnopqrstuvw" + "x");

        // TimberExceptionLogging
        Timber.d(new Exception(), new Exception().getMessage());
        Timber.d(new Exception(), "");
        Timber.d(new Exception(), null);
        Timber.d(new Exception().getMessage());
    }

    String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    List<String> mPermissionList = new ArrayList<>();

    private static final int PERMISSION_REQUEST = 1;
    private void checkPermission() {
        mPermissionList.clear();

        //判断哪些权限未授予
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        /**
         * 判断是否为空
         */
        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了

        } else {//请求权限方法
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST);
        }
    }

    /**
     * 响应授权
     * 这里不管用户是否拒绝，都进入首页，不再重复申请权限
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST:

                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                break;
        }
    }
}
